<?php
namespace Admin\Controller;
use Common\Controller\BackendController;

// +----------------------------------------------------------------------
// | Copyright (c) 2015 http://www.itaoke.org All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author:  Caption <Caption@163.com> QQ 1674653
// +----------------------------------------------------------------------

class ShareController extends BackendController {
    public function _initialize() {
        parent::_initialize(); 
        
        $this->_mod = M('UserShare');
        
        $api_config = M('ItemsSite')->where(array('code' => 'itaoke'))->getField('config');
        $this->_tbconfig = unserialize($api_config);
    }
    public function index() {
        $this->display();
    }
    
	public function check(){
		$_dbpre=C("DB_PREFIX");
		import("ITK.ORG.Page");
		
		if (I('orderid', '', 'trim')) {
			$where['a.orderid'] = I('orderid', '', 'trim');
		} 
		$this->assign('orderid',$where['a.orderid']);
		
		$count = $this->_mod->alias("a")->where($where)->count();
		$pager = new \Page($count, 20);
		$res=$this->_mod->alias("a")->join("{$_dbpre}user as b on a.uid=b.id")->field("a.*,b.username")->where($where)->order("a.id desc")->limit($pager->firstRow.','.$pager->listRows)->select();
		$page = $pager->show();
		$this->assign('page',$page);
		$this->assign('list',$res);
		$this->display();
	}
	
	public function delete(){
		if(!IS_AJAX) return;
//		$id = $this->_post('id', 'trim');
		$pk = $this->_mod->getPk();
		$ids = trim(I($pk), ',');
		!$ids && $this->ajaxReturn(1,'参数错误');
		$where['id'] = array('in', $ids);

		$this->_mod->where($where)->delete();
		$this->ajaxReturn(0,L('operation_success'));
	}
    
	/**
	 * 一键审核
	 */
	public function do_check(){
		if(!IS_AJAX) return;
		$pk = $this->_mod->getPk();
		$ids = trim(I($pk), ',');
		$datas['id'] = array('in', $ids);
		$datas['status'] = 1;
		if ($datas) {
			if (false !== $this->_mod->save($datas)) {
				//处理积分
				$_ids=explode(',', $ids);
				for($i=0;$i<count($_ids);$i++)
					$this->dealUserScore($_ids[$i],1,C('itk_score_rule.share_score'));
				
				IS_AJAX && $this->ajaxReturn(1, L('operation_success'));
			} else {
				IS_AJAX && $this->ajaxReturn(0, L('operation_failure'));
			}
		} else {
			IS_AJAX && $this->ajaxReturn(0, L('illegal_parameters'));
		}
	}
	
	public function getcheck()
	{
		if (IS_POST) {
			$id = $this->_post('id', 'trim');
			$status = $this->_post('status', 'trim');
			$score = $this->_post('score', 'trim');
			$reason = $this->_post('reason', 'trim');
			
			if($score==''||$score==0){
				$score=C('itk_score_rule.share_score');
			}
			if ($status == 0) {
				$check_date['status'] = $status;
				$this->_mod->where(array('id' => $id))->save($check_date);
				$this->success(L('operation_success'));
			} else {
				if ($status == 2) {	// 失败扣除积分
					$check_date['status'] = $status;
					$check_date['reason'] = $reason;
					$check_date['score'] = $score;
					$check_date['verify_time'] = time();
					$this->_mod->where(array('id' => $id))->save($check_date);

				} else {
					if ($status == 1) {		// 成功加积分
						$check_date['status'] = $status;
						$check_date['score'] = $score;
						$check_date['verify_time'] = time();
						$this->_mod->where(array('id' => $id))->save($check_date);
					}
				}
				$this->dealUserScore($id,$status,$score);
				$this->success( L('operation_success'));
			}
		} else {
			$id = $this->_get('id', 'trim');
			!$id && $this->ajaxReturn(0, 'ID错误，请刷新');
			$share = $this->_mod->where(array('id' => $id))->find();
			$this->assign('share', $share);
			$resp = $this->fetch();
			$this->ajaxReturn(1, '审核管理', $resp);
		}
	}
	
	/**
	 * 用于处理用户的晒单积分策略
	 * @param unknown $id
	 * @param unknown $type
	 */
	private function dealUserScore($id,$status,$score){
		$uid=$this->_mod->getFieldById($id,'uid');
//		if($_status==$status) return;
		
		$_ScoreLog_mod = D('ScoreLog');
		$user_mod=M('User');
		$username=$user_mod->getFieldById($uid,'username');
		//改变用户积分
		if($status==2){
			$ScoreLog_data['action']	=	'sharefail';
			$score_data = array('score'=>array('exp','score-'.$score));
		}
		else {
			$score_data = array('score'=>array('exp','score+'.$score));
			$ScoreLog_data['action']	=	'sharesuccess';
		}
		$user_mod->where(array('id'=>$uid))->setField($score_data); //改变用户积分
		
		//添加一条积分记录
		$ScoreLog_data['uid']		=	$uid;
		$ScoreLog_data['uname']	=	$username;
		$ScoreLog_data['score']	=	$score;
		$_ScoreLog_mod->create($ScoreLog_data);
		$_ScoreLog_mod->add();
		
	}
	
} 
?>
